//
//  XYJMineViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/16.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJMineViewController.h"
#import "XYJMineHeaderView.h"
#import "SZKImagePickerVC.h"
#import "XYJFSViewController.h"
#import "XYJGZViewController.h"
#import "XYJXHViewController.h"
#import "XYJSCViewController.h"
#import "SelectedView.h"
#import "XYJSettingViewController.h"
#import "HWPopTool.h"

@interface XYJMineViewController ()<XYJMineHeaderViewDelegate,SZKImagePickerVCDelegate,UIScrollViewDelegate>{
    UITableView *_tableView;
}

@property (nonatomic,strong) XYJMineHeaderView *headerView;

@property (nonatomic,assign) BOOL isChange;

@property (nonatomic,strong) UIImageView *erWeiMaView;

@property (nonatomic,assign) BOOL isLogin;

@property (nonatomic,strong) UIView *backView;

@property (nonatomic,assign) BOOL isBigimage;

@property (nonatomic,assign) BOOL isChooseImage;

@property (nonatomic,strong) UITapGestureRecognizer *ges;

@property (nonatomic,strong) UIView *statusBarView;

@property (nonatomic,strong) UIScrollView *backScroller;


@property (nonatomic,strong) NSArray *titleImgArr;
@property (nonatomic,strong) NSArray *titltTextArr;

@property (strong,nonatomic) UIView *contentView;
@property (strong,nonatomic) UIButton *popBtn;

@end

@implementation XYJMineViewController


-(instancetype)init{
    if (self = [super init]) {
        self.ges = [[UITapGestureRecognizer alloc] init];
        _statusBarView = [[UIView alloc] init];
    }
    return self;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //赋初始值
    _isBigimage = YES;
    _isChooseImage = NO;
    //初始化scrollView
    [self backScroller];
    //初始化视图
    [self setupTopView];
    //初始化二维码
    [self erWeiMaView];
    self.navigationController.navigationBarHidden = YES;
    //添加手势
    self.view.userInteractionEnabled = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(not) name:@"clearIconImg" object:nil];
}

-(void)imgs:(NSNotification *)not{
    [_headerView.headImageView setImage:[SZKImagePickerVC loadImageFromSandbox:@"headImage"]];
}


-(void)not{
    UIImage *sandBoxImage = [SZKImagePickerVC loadImageFromSandbox:@"headImage"];
    sandBoxImage?[_headerView.headImageView setImage:sandBoxImage]:[_headerView.headImageView setImage:[UIImage imageNamed:@"logo"]];
    [_headerView.headImageView setImage:[UIImage imageNamed:@"logo"]];
}


//#pragma mark - dealloc
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"123" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"clearIconImg" object:nil];
}

-(void)setupTopView{
    _headerView = [[XYJMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 210)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(not) name:@"clearIconImg" object:nil];
    _headerView.delegate = self;
    [self.view addSubview:_headerView];
    
    
    
    XYJFSViewController *FSVC = [[XYJFSViewController alloc] init];
    XYJGZViewController *GZVC = [[XYJGZViewController alloc] init];
    XYJSCViewController *SCVC = [[XYJSCViewController alloc] init];
    XYJXHViewController *XHVC = [[XYJXHViewController alloc] init];
    
    NSArray *nameArr = @[@"收藏",@"喜欢",@"粉丝",@"关注"];
    NSArray *VCArr = @[SCVC,XHVC,FSVC,GZVC];
    
    SelectedView *selectedView = [[SelectedView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headerView.frame), SCREEN_WIDTH, SCREEN_HEIGHT - _headerView.frame.size.height - 48) buttonName:nameArr controllers:VCArr parentController:self];
    [self.view addSubview:selectedView];
    
}


//点击事件
//二维码
-(void)erWeiMa{
    self.isLogin = YES;
    if (self.isLogin == YES) {
        //1.实例化二维码滤镜
        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        //2.恢复滤镜的默认属性(因为滤镜有可能保存上一次的属性)
        [filter setDefaults];
        //3.将字符串转换成NSData
        NSData *data = [@"http://www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding];
        //4.通过KVO设置滤镜，传入data，将来滤镜就知道要通过传入的数据生成二维码
        [filter setValue:data forKey:@"inputMessage"];
        //5.生成二维码
        CIImage *outputImage = [filter outputImage];
        
        UIImage *image = [UIImage imageWithCIImage:outputImage];
        //6.设置生成好的二维码到imageView上
        self.erWeiMaView.image = image;
        
        //添加弹出窗口
        _contentView = [[UIView alloc] initWithFrame:RECT_MAKE((SCREEN_WIDTH - 150) / 2, (SCREEN_HEIGHT - 150) / 2, 150, 150)];
        _contentView.backgroundColor = [UIColor clearColor];
        [self popViewShow];
        
        self.erWeiMaView.hidden = NO;
        self.ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancel)];
        [self.backScroller addGestureRecognizer:self.ges];

    }
}


//弹出二维码
-(void)popViewShow{
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:_contentView.bounds];
    imageV.image = self.erWeiMaView.image;
    [_contentView addSubview:imageV];
    
    //看看pop效果把下面一句话加上
//    [_contentView addSubview:_popBtn];
    
    [HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid;
    [[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES];
}


//点击设置
-(void)setting{
    NSLog(@"点击设置");
    XYJSettingViewController *settingVC = [[XYJSettingViewController alloc] init];
    self.navigationController.navigationBarHidden = NO;
    settingVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:settingVC animated:YES];
}

//头像
-(void)changeImage{
    NSLog(@"点击头像");
    if (!_isChange) {
        [self setupAlert];
    }else{
        [self smallImage];
    }
}

-(void)setupAlert{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"基本操作" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *ImagePicker = [UIAlertAction actionWithTitle:@"更换头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self ImagePickerAlert];
    }];
    
    UIAlertAction *bigImage = [UIAlertAction actionWithTitle:@"查看大头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击查看大头像");
        [self bigImage];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertC addAction:ImagePicker];
    [alertC addAction:bigImage];
    [alertC addAction:cancel];
    
    [self presentViewController:alertC animated:YES completion:nil];
}


-(void)cancel{
    self.erWeiMaView.hidden = YES;
    self.isLogin = NO;
    [self.backScroller removeGestureRecognizer:self.ges];
}


#pragma mark - 查看大头像
-(void)bigImage{
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_backView];
    self.tabBarController.tabBar.hidden = YES;
    
    [UIView animateWithDuration:0.5 animations:^{
        _headerView.headImageView.frame = CGRectMake(0, (SCREEN_HEIGHT - 250) / 2, SCREEN_WIDTH, 250);
        _headerView.headImageView.layer.cornerRadius = 0;
        _headerView.headImageView.layer.masksToBounds = YES;
        [_backView addSubview:_headerView.headImageView];
    }];
    _isChange = YES;
}


#pragma mark - 缩小头像
-(void)smallImage{
    [_backView removeFromSuperview];
    [UIView animateWithDuration:0.5 animations:^{
        _headerView.headImageView.frame = CGRectMake((_headerView.bounds.size.width - 80) / 2, (_headerView.bounds.size.height - 80) / 2, 80, 80);
        _headerView.headImageView.layer.cornerRadius = 40;
        _headerView.headImageView.layer.masksToBounds = YES;
        [_headerView addSubview:_headerView.headImageView];
    }];
    _isChange = NO;
    self.tabBarController.tabBar.hidden = NO;
}




#pragma mark - 更换头像
-(void)ImagePickerAlert{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"更换头像" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self presentViewController:ImagePickerStyleCamera];
        }else{
            NSLog(@"该设备不支持相机功能");
        }
    }];
    
    UIAlertAction *xiangce = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self presentViewController:ImagePickerStylePhotoLibrary];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertC addAction:camera];
    [alertC addAction:xiangce];
    [alertC addAction:cancel];
    
    [self presentViewController:alertC animated:YES completion:nil];
}


#pragma mark - SZKImagePickerVC
//初始化相机
-(void)presentViewController:(imagePickerStyle)style{
    SZKImagePickerVC *picker = [[SZKImagePickerVC alloc] initWithImagePickerStyle:style];
    picker.SZKDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - SZKImagePickerVCDelegate
-(void)imageChooseFinish:(UIImage *)image{
    [_headerView.headImageView setImage:image];
    //保存到沙盒中
    [SZKImagePickerVC saveImageToSandbox:image andImageNage:@"headImage" andResultBlock:^(BOOL success) {
        NSLog(@"保存成功");
    }];
}


#pragma mark - 懒加载
//二维码
-(UIImageView *)erWeiMaView{
    if (_erWeiMaView == nil) {
        _erWeiMaView = [[UIImageView alloc] initWithFrame:RECT_MAKE((SCREEN_WIDTH - 150) / 2, (SCREEN_HEIGHT - 150) / 2, 150, 150)];
        _erWeiMaView.backgroundColor = [UIColor whiteColor];
        _erWeiMaView.hidden = YES;
    }
    return _erWeiMaView;
}


@end
